//
//  RootViewController.m
//  iVoc
//
//  Created by Wallace Wu on 09-3-5.
//  Copyright Sonic Solutions 2009. All rights reserved.
//

#import "VocListController.h"
#import "iVocAppDelegate.h"
#import "VOCWordList.h"
#import "VOCMutableWordList.h"
#import "VOCSingleListViewController.h"
#import "VOCWordTestViewController.h"

#define SECTION_COMMON              0
#define SECTION_FAVORITE            1

@implementation VocListController

@dynamic wordLists;

-(NSArray*)wordLists
{
    if(!wordLists)
    {
        wordLists = [[VOCWordList allWordLists:VOC_DEFAULT_WORDLIST_PATH] retain];
    }
    
    return wordLists;
}

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
    self = [super initWithNibName:nibName bundle:nibBundle];
    if (self) {
        
        self.title = NSLocalizedString(@"WORDLISTS",nil);
        self.tabBarItem.image = [UIImage imageNamed:@"wordList.png"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == SECTION_COMMON)
    {
        return [self.wordLists count];
    }
    else
    {
        return 1;
    }
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    NSUInteger section = [indexPath indexAtPosition:0];
    VOCWordList *tmp = nil;
    if(section == SECTION_COMMON)
    {
        NSUInteger index = [indexPath indexAtPosition:1];
        tmp = [self.wordLists objectAtIndex:index];
    }
    else
    {
        tmp = [VOCMutableWordList favoriteList];
    }
    
    // Set up the cell...
    [cell setText:tmp.name];

    return cell;
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView 
         accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellAccessoryNone; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    
    NSInteger section = [indexPath indexAtPosition:0];
    
    if(section == SECTION_COMMON)
    {
        selectedList = [self.wordLists objectAtIndex:[indexPath indexAtPosition:1]];  
    }
    else
    {
        selectedList = [VOCMutableWordList favoriteList];
    }

    if([self isMemberOfClass:[VocListController class]])
    {
        VOCSingleListViewController *singleListViewController = [[VOCSingleListViewController alloc] initWithNibName:@"VOCSingleListViewController" bundle:nil];
        singleListViewController.list = selectedList;
        [self.navigationController pushViewController:singleListViewController animated:YES];
        [VOCSingleListViewController release];
    }
     
}

- (void)dealloc {
    [selectedList release];
    [self.wordLists release];
    [super dealloc];
}


@end

